package com.android.fastride.model.sharedpreference;

import android.content.Context;
import android.content.SharedPreferences;

import com.android.fastride.FastRideApplication;


/**
 * @author dzl
 *  公共共享参数文件
 */
public class SPreferBase {

	/**
	 * 存放配置信息的文件名称
	 */
	private final static String CONFIG_FILENAME = "user_config.xml";
	/**
	 * 用户登录相关配置：是否为用户名密码登录
	 */
	@Deprecated
	public final static String BENGUO_CONF_IS_USERNAME_PWD = "benguo_conf_is_username_pwd";

	/**
	 * 用户登录相关配置：是否为自动登录<br/>
	 * 登录成功并成功获取用户信息时，设为true；注销登录时，设为false
	 */
	public final static String BENGUO_CONF_IS_AUTO_LOGIN = "benguo_conf_is_auto_login";
	/**
	 * 用户登录相关配置：手机号
	 */
	public final static String BENGUO_CONF_PHONE = "benguo_conf_phone";
	/**
	 * 用户登录相关配置：密码或验证码
	 */
	public final static String BENGUO_CONF_PASSWORD = "benguo_conf_password";
	/**
	 * 用户登录相关配置：设备号
	 */
	public final static String BENGUO_CONF_DEVID = "benguo_conf_devid";
	/**
	 * 用户登录相关配置：token
	 */
	public final static String BENGUO_CONF_TOKEN = "benguo_conf_token";

	/**
	 * 用户第一次使用
	 */
	public final static String BENGUO_CONF_FIRST_USE = "benguo_conf_first_use";

	public final static String BENGUO_USER_INFO = "benguo_user_info";


	private static SPreferBase mInstance;
	public static SPreferBase getInstance() {
		if (mInstance == null) {
			synchronized (SPreferBase.class) {
				if (mInstance == null) {
					mInstance = new SPreferBase();
				}
			}
		}
		return mInstance;
	}

	private SharedPreferences sp;
	
	private SPreferBase() {
		//Context.MODE_MULTI_PROCESS 虽然可以共享数据，但是容易出错
		sp = FastRideApplication.getApp().getSharedPreferences(
				CONFIG_FILENAME, Context.MODE_PRIVATE);
	}

	/**
	 * 获取公共共享信息
	 * 
	 * @return 返回配置信息的值
	 */
	public String getStringKey(String key, String delValue) {
		return sp.getString(key, delValue);
	}

	public boolean getBooleanKey(String key, boolean delValue) {
		return sp.getBoolean(key, delValue);
	}

	public int getIntKey(String key, int delValue) {
		return sp.getInt(key, delValue);
	}

	public long getLongKey(String key, long delValue) {
		return sp.getLong(key, delValue);
	}

	public float getFloatKey(String key, float delValue) {
		return sp.getFloat(key, delValue);
	}

	/**
	 * 设置公共共享信息
	 * 
	 * @return 返回配置信息的值
	 */
	public synchronized void setStringKey(String key, String value) {
		sp.edit().putString(key, value).commit();
	}

	public synchronized void setBooleanKey(String key, boolean value) {
		sp.edit().putBoolean(key, value).commit();
	}

	public synchronized void setIntKey(String key, int value) {
		sp.edit().putInt(key, value).commit();
	}

	public synchronized void setLongKey(String key, long value) {
		sp.edit().putLong(key, value).commit();
	}

	public synchronized void setFloatKey(String key, float value) {
		sp.edit().putFloat(key, value).commit();
	}

	/**
	 * 多进程共享SharedPreferences
	 * 
	 * @param key
	 * @param delValue
	 * @return
	 */
	public boolean getBoolean(String key, boolean delValue) {
		return sp.getBoolean(key, delValue);
	}

	/**
	 * 多进程共享SharedPreferences
	 * 
	 * @param key
	 * @param delValue
	 * @return
	 */
	public String getString(String key, String delValue) {
		return sp.getString(key, delValue);
	}

	
	/**
	 * 删除键值
	 * 
	 * @param key
	 */
	public synchronized void remove(String key) {
		sp.edit().remove(key).commit();
	}

	/**
	 * 清空配置(不包括手机号，用于登录界面回显)
	 */
	public synchronized void clear() {
		String phone = sp.getString(BENGUO_CONF_PHONE, "");
		sp.edit().clear().putString(BENGUO_CONF_PHONE, phone).commit();
	}

	/**
	 * 是否已登录
	 * @return
	 */
	public boolean isLogin() {
		if (getBooleanKey(BENGUO_CONF_IS_AUTO_LOGIN, false)
				&& ((getStringKey(BENGUO_CONF_DEVID, "").length() > 0
				&& getStringKey(BENGUO_CONF_TOKEN, "").length() > 0)
				|| (getStringKey(BENGUO_CONF_PHONE, "").length() > 0
				&& getStringKey(BENGUO_CONF_PASSWORD, "").length() > 0))) {
			return true;
		}
		return false;
	}
}
